package cn.yunyichina.scheduler.jobs.thread.task;

import cn.yunyichina.provider.framework.dubbo.service.PayService;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.scheduler.jobs.constant.MethodRouter;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * his账单及平台流水对账线程
 * Created by JoeZ on 2016/11/20.
 */
public class HisPayfinanceRunnable implements Callable<Response> {

    private Logger logger = LoggerFactory.getLogger(HisPayfinanceRunnable.class);

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private String branchCode;
    private String hospitalCode;
    private String beginPayTime;
    private String endPayTime;
    private String payMode;
    private String orgId;
    private String orgName;

    public HisPayfinanceRunnable(String orgId , String orgName,String branchCode, String hospitalCode, String beginPayTime, String endPayTime, String payMode) {
        this.branchCode = branchCode;
        this.hospitalCode = hospitalCode;
        this.beginPayTime = beginPayTime;
        this.endPayTime = endPayTime;
        this.payMode = payMode;
        this.orgId = orgId;
        this.orgName = orgName;
    }

    @Override
    public Response call() throws Exception {
        return inputAndCheckPayments();
    }


    public Response inputAndCheckPayments(){
        Map<String,String > map = new HashMap<String,String >();

        long time = new Date().getTime() / 1000;

        try {
            //获取his账单前数据清理
            map.put("orgId" , orgId);
            map.put("payTime" , beginPayTime);
            map.put("isClearHis" , "1");
            Response restResp = PayService.callHumpFromInternal(MethodRouter.HIS_PAYMENT_PLATFORM_RESET, JSON.toJSONString(map));
            logger.info(orgName + "his数据清理返回结果：  " + restResp.getResultCode() + " -- " + restResp.getResultMessage());
        } catch (Exception e) {
            logger.error("[HisPayfinanceRunnable.inputAndCheckPayments].his数据清理失败！", e);
        }


        logger.info("<<<<< HisPayfinanceRunnable."+ orgName +"开始时间:" + sdf.format(new Date()));
        map = new HashMap<String,String >();
        Response response = new Response();
        map.put("branchCode", branchCode);
        map.put("hospitalCode", hospitalCode);
        map.put("beginPayTime", beginPayTime + " 00:00:00"); //应项目要求修改账单拉取时间规则
        map.put("endPayTime", beginPayTime + " 23:59:59");
        map.put("payMode", payMode);
        map.put("orgId", orgId);
        map.put("orgName", orgName);

        logger.info(orgName +"<<<<< HisPayfinanceRunnable.请求前置机时间:" + ((new Date().getTime() / 1000) - time));

        Response resp = PayService.callHumpFromInternal(MethodRouter.HIS_CHECK_PAYMENT_PLATFORM, JSON.toJSONString(map));
        response.setResult(orgId);
        response.setResultCodeAndMessage(resp.getResultCode() , resp.getResultMessage());

        logger.info("<<<<< HisPayfinanceRunnable."+ orgName +"结束时间:" + ((new Date().getTime() / 1000) - time));
        return response;
    }
}
